import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { listSimpleDictData } from '@/api/system/dict'

const dict = {
  state: {
	dictMap: storage.get(constant.dictCache),
	isSetDict: false
  },

  mutations: {
    SET_DICTMAP: (state, dictMap) => {
      state.dictMap = dictMap
      storage.set(constant.dictCache, dictMap)
    },
	SET_ISSETDICT: (state, isSetDict) => {
	  state.isSetDict = isSetDict
	},
  },

  actions: {

	// 获取所有字典项
	GetDictMap({ commit, state }) {
		console.log('获取所有字典')
		return new Promise((resolve, reject) => {
		  listSimpleDictData().then(res => {
		    const data = res.data; // 读取 data 数据
			// 设置数据
			const dictDataMap = new Map()
			data.forEach((dictData) => {
				// 获得 dictType 层级
				const enumValueObj = dictDataMap[dictData.dictType]
				if (!enumValueObj)
				dictDataMap[dictData.dictType] = []
	
				// 处理 dictValue 层级
				dictDataMap[dictData.dictType].push({
				value: dictData.value,
				label: dictData.label,
				colorType: dictData.colorType,
				cssClass: dictData.cssClass,
				})
			})
			commit('SET_DICTMAP', dictDataMap)
			commit('SET_ISSETDICT', true)
		    resolve(res)
		  }).catch(error => {
		    reject(error)
		  })
		})
	},
	
  }
}

export default dict
